package com.shujia.flink.tf

import org.apache.flink.streaming.api.functions.ProcessFunction
import org.apache.flink.streaming.api.scala._
import org.apache.flink.util.Collector

object Demo8Process {
  def main(args: Array[String]): Unit = {
    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    val linesDS: DataStream[String] = env.socketTextStream("master", 8888)

    /**
     * process: 是flink一个底层的算子
     */
    val wordsDS: DataStream[String] = linesDS.process(new ProcessFunction[String, String] {
      /**
       * processElement:相当大flatmap,用于处理数据，进来一行可以返回多行
       * process可以代替，map flatmap,filter的功能
       *
       * @param value ：一行数据
       * @param ctx   ：上下文对象
       * @param out   ：用于将数据发送到下游
       */
      override def processElement(value: String,
                                  ctx: ProcessFunction[String, String]#Context,
                                  out: Collector[String]): Unit = {
        val split: Array[String] = value.split(",")
        for (word <- split) {
          //将数据发送到下游
          out.collect(word)
        }
      }
    })

    wordsDS.print()

    env.execute()
  }

}
